import scrapy
from scrapy import Request
import mysql.connector
from scrapyspider.items import Sponsor_Continue

class sponsor_idSpider(scrapy.Spider):
    name = "sponsor_id"
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
    }

    def start_requests(self):
        conn = mysql.connector.connect(host='120.79.172.200', password='icontest', database='icontest', user='icontest')
        cursor = conn.cursor()
        cursor.execute("select url from urls")
        for i in cursor:
            url = "".join(i)
            yield Request(url, headers=self.headers)
        conn.commit()
        cursor.close()
        conn.close()

    def parse(self, response):
        item = Sponsor_Continue()
        sponsor_name = response.xpath('normalize-space(//div[@class="sidebar-b-con m-10"][2]//li[@class="new-event4-1-info-item clearfix"]//div/text())').extract()
        contest_name = response.xpath('//h1[@class="event-title"]/text()').extract()
        item['sponsor_name'] = "".join(sponsor_name)
        item['contest_name'] = "".join(contest_name)
        yield  item